package com.example.springstudy.controller;

import com.example.springstudy.base.Result;
import com.example.springstudy.base.ResultCode;
import com.example.springstudy.base.annotation.SaveLog;
import com.example.springstudy.dao.res.LoginRes;
import com.example.springstudy.entity.LogEntity;
import com.example.springstudy.service.AuthService;
import com.example.springstudy.service.LogService;
import com.example.springstudy.utils.RequestUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
@Slf4j
public class AuthController {

    @Autowired
    AuthService  authService;

    @SaveLog("登录")
    @Operation(summary = "登录认证")
    @PostMapping("/login")
    public Result<LoginRes> login(@RequestParam String username, @RequestParam String password) {
        try{
            LoginRes res = authService.login(username, password);
            return Result.success(res);
        } catch (Exception e){
            log.error(e.getMessage(), e);
            return Result.failed(ResultCode.PARAM_ERROR, e.getMessage());
        }
    }

    @SaveLog("退出登录")
    @Operation(summary = "退出登录！")
    @GetMapping("/logout")
    public Result<Boolean> logout(){
        try{
            authService.logout();
            return Result.success();
        }  catch (Exception e){
            return Result.failed(ResultCode.getValue("A0001"), e.getMessage());
        }
    }
}
